<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\service\MenuService;
use app\services\system\attachment\SystemAttachmentService;
use services\UploadService;
use think\facade\Cache;

class Ajax extends AdminController
{

    /**
     * 初始化后台接口地址
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function initAdmin()
    {
        $cacheData = Cache::get('initAdmin_' . session('admin.id'));
        if (!empty($cacheData)) {
            return json($cacheData);
        }
        $menuService = new MenuService(session('admin.id'));
        $data        = [
            'logoInfo' => [
                'title' => sysconfig( 'site_name'),
                'image' => sysconfig( 'site_logo'),
                'href'  => __url('index/index'),
            ],
            'homeInfo' => $menuService->getHomeInfo(),
            'menuInfo' => $menuService->getMenuTree(),
        ];
        Cache::tag('initAdmin')->set('initAdmin_' . session('admin.id'), $data);
        return json($data);
    }

    /**
     * 清理缓存接口
     */
    public function clearCache()
    {
        Cache::clear();
        $this->success('清理缓存成功');
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        $data = [
            'is_save' => $this->request->param('is_save',1)
        ];
        // 上传类型
        $uploadType = (int)sysconfig('upload_type', 1);
        $upload     = UploadService::init($uploadType);
        $info       = $upload->to('system')->validate()->move();
        if ($info === false) {
            $this->error($upload->getError());
        }
        $res = $upload->getUploadInfo();
        if ($data['is_save']) {
            /** @var SystemAttachmentService $attachmentService */
            $attachmentService = app()->make(SystemAttachmentService::class);
            $attachmentService->attachmentAdd($res['real_name'], $res['size'], $res['type'], $res['dir'], $res['full_path'], 1, $uploadType, $res['time']);
        }
        $this->success('上传成功！', ['url' => $res['full_path']]);
    }

    /**
     * 上传图片至编辑器
     * @return \think\response\Json
     */
    public function uploadEditor()
    {
        $data = [
            'upload_type' => $this->request->post('upload_type'),
            'file'        => $this->request->file('upload'),
        ];
        // 上传类型
        $uploadType = (int)sysconfig('upload_type', 1);
        $upload     = UploadService::init($uploadType);
        $info       = $upload->to('system')->validate()->move();
        if ($info === false) {
            $this->error($upload->getError());
        }
        $res = $upload->getUploadInfo();
        return json([
            'error'    => [
                'message' => '上传成功',
                'number'  => 201,
            ],
            'fileName' => '',
            'uploaded' => 1,
            'url'      => $res['full_path'],
        ]);
    }
}